package gameobjects;

import java.util.ArrayList;
import java.util.regex.*;

public class Compiler {
	public int pointer;
	private ArrayList<Citem> clist;
	private boolean usefgo;
	private String code[];
	
	public Compiler(String[] antCode){ 
		usefgo = false;
		clist = new ArrayList<Citem>();
		pointer = 0;
		code = antCode;
	    
	    Pattern goPat = Pattern.compile("^f?go\\w{1,10}");  
	    Pattern conditionPat = Pattern.compile("(caneat|canmove|see.food|see.ant|see.wall|canbite|true)");  
	    Pattern actionTPat = Pattern.compile("do\\s+(move|bite|eat|rotate|reverse)");  
	    Pattern goTPat = Pattern.compile("do\\s+(move|bite|eat|rotate|reverse)\\s+f?go\\w{1,10}");  
	    Pattern actionFPat = Pattern.compile("else\\s+(move|bite|eat|rotate|reverse)");  
	    Pattern goFPat = Pattern.compile("else\\s+(move|bite|eat|rotate|reverse)\\s+f?go\\w{1,10}");  
	    Matcher goMtc;
	    Matcher conditionMtc;
	    Matcher goTMtc;
	    Matcher actionTMtc;
	    Matcher goFMtc;
	    Matcher actionFMtc;
	    
	    for(int i = 0; i < code.length; i++){
		    goMtc = goPat.matcher(code[i]);
		    conditionMtc = conditionPat.matcher(code[i]);
		    goTMtc = goTPat.matcher(code[i]);
		    actionTMtc = actionTPat.matcher(code[i]);
		    goFMtc = goFPat.matcher(code[i]);
		    actionFMtc = actionFPat.matcher(code[i]);
		    boolean b = goMtc.find();
		    String igo = "";
		    if (b){
		    	igo = goMtc.group(0);
		    }
		    
		    b = conditionMtc.find();
		    String icondition = "";
		    
		    if(b){
		    	icondition = conditionMtc.group(0);
		    }
		    
		    b = actionTMtc.find();
		    String iactionT = "";
		    
		    if(b){
		    	iactionT = actionTMtc.group(0);
		    	iactionT = iactionT.split("\\s")[1];
		    }
		    
		    b = goTMtc.find();
		    String igoT = "";
		    
		    if(b){
		    	igoT = goTMtc.group(0);
		    	igoT = igoT.split("\\s")[2]; 
		    }
		    
		    b = actionFMtc.find();
		    String iactionF = "";
		    
		    if(b){
		    	iactionF = actionFMtc.group(0);
		    	iactionF = iactionF.split("\\s")[1];
		    }
		    
		    b = goFMtc.find();
		    String igoF = "";
		    
		    if(b){
		    	igoF = goFMtc.group(0);
		    	igoF = igoF.split("\\s")[2]; 
		    }
		    
		    clist.add(new Citem(icondition,iactionT,igoT,iactionF,igoF,igo));
	    }
	    
	}
	
	public String nextaction(boolean flag){
		if(pointer == clist.size()) pointer = 0;
		int tmppoint = pointer;
		boolean fgo  = clist.get(pointer).go.contains("fgo");
		if(fgo && flag && usefgo){
			pointer++;
			if(pointer == clist.size()) pointer = 0;
			usefgo = false;
			if(clist.get(tmppoint).goT != ""){
				for(int i = 0; i < clist.size();i++){
					if(clist.get(i).go.equals(clist.get(tmppoint).goT)){
						if(clist.get(i).go.contains("fgo")){
							usefgo = true;
						}
						pointer = i;
					}
				}
			}
			return clist.get(tmppoint).actionT;
		}
		else if(clist.get(tmppoint).condition.equals("true")){
			pointer++;
			if(pointer == clist.size()) pointer = 0;
			return"";
		}
		if (flag){ 
			if(clist.get(pointer).goT != ""){
				for(int i = 0; i < clist.size();i++){
					if(clist.get(i).go.equals(clist.get(pointer).goT)){
						if(clist.get(i).go.contains("fgo")){
							usefgo = true;
						}
						pointer = i;
						return clist.get(tmppoint).actionT;
					}
				}
			}
			else pointer++;
			if(pointer == clist.size()) pointer = 0;

			return clist.get(tmppoint).actionT;
			}
		
		if(clist.get(pointer).goF != ""){
			for(int i = 0; i < clist.size();i++){
				if(clist.get(i).go.equals(clist.get(pointer).goF)){
					if(clist.get(i).go.contains("fgo")){
						usefgo = true;
					}
					pointer = i;
					return clist.get(tmppoint).actionF;
				}
			}
		}
		else pointer++;
		if(pointer == clist.size()) pointer = 0;

		return clist.get(tmppoint).actionF;
		
	} 
	
	public String condition(){
		return clist.get(pointer).condition;
	}
	
	/*
	public static  void main(String[] argv ){
		Compiler a = new Compiler();
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(false));
		System.out.println(a.nextaction(false));
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(true));
		System.out.println(a.nextaction(true));
	}*/
}


class Citem{
	public String go;
	public String condition;
	public String actionT;
	public String goT;
	public String actionF;
	public String goF;
	
	public Citem(String a, String b, String c, String d, String e, String g){
		condition = a;
		actionT = b;
		goT = c;
		actionF = d;
		goF = e;
		go = g;
	}
}